package com.szty.libarytest.other

import android.graphics.Bitmap
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.szty.libarytest.R
import kotlinx.android.synthetic.main.activity_crop_img.*

class CropImgActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_crop_img)
        val imgAddress =
            "https://test.images.shenzhoutianyu.com/exam/card/20210422/e093731b44429488b0318fc364ef3b96.png"
        Glide.with(this).asBitmap().load(imgAddress).into(object : SimpleTarget<Bitmap>() {
            override fun onResourceReady(
                resource: Bitmap,
                transition: Transition<in Bitmap>?
            ) {
                img_original.setImageBitmap(resource)
                img_crop.setImageBitmap(cropImg(resource, 146, 750, 114, 970))
            }
        })
    }

    fun cropImg(bitmap: Bitmap, height: Int, width: Int, startX: Int, startY: Int): Bitmap {
        bitmap.width.loge()
        bitmap.height.loge()
        return Bitmap.createBitmap(bitmap, startX, startY, width, height, null, false)

    }
}

fun Any.loge(tag:String? = "") {
    Log.e(
        if(tag.isNullOrEmpty())"SunJH" else tag, this.toString()
    )
}